package com.ztx.service.resource.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztx.model.ResourceVO;
import com.ztx.pagemodel.SessionInfo;
import com.ztx.pagemodel.easyui.Tree;
import com.ztx.service.resource.IResourceService;
import com.ztx.utils.Constants;

@Service(IResourceService.BEAN_ID)
public class ResourceServiceImpl implements IResourceService {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;

	public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
		this.sqlSessionTemplate = sqlSessionTemplate;
	}

	@Override
	public List<Tree> tree(SessionInfo sessionInfo) {
		List<ResourceVO> resources = null;
		List<Tree> listTree = new ArrayList<Tree>();
		Map<String, String> params = new HashMap<String, String>();
		params.put("resourceTypeId", Constants.RESOURCE_MENU);// 菜单资源类型
		params.put("userId", sessionInfo.getUser().getId());

		resources = sqlSessionTemplate.selectList(ResourceVO.class.getName()
				+ ".getResourceByUserid", params);

		if (resources != null && resources.size() > 0) {
			for (ResourceVO r : resources) {
				Tree tree = new Tree();
				BeanUtils.copyProperties(r, tree);
				tree.setText(r.getName());
				tree.setIconCls(r.getIcon());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.getUrl());
				tree.setAttributes(attr);
				listTree.add(tree);
			}
		}
		return listTree;
	}

}
